home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / index.cq / index.c
Text File  |  1985-05-30  |  7KB  |  230 lines

  1. /* 
  2.    This program indexes a text file. 
  3.    author: Greg Haley
  4.    revision date: 04/02/85
  5.    A text file containing a word list is used as input.
  6.    Each word to index on is on a seperate line. Example:
  7.        byte
  8.          directive
  9.         SAMPLE
  10.    An index file will be created using the words byte, directive, and sample
  11.    to index on. The words may be entered in upper or lower case.
  12.    Pages are seperated by the ^L character (ASCII form feed) in the original
  13.    text.
  14.  
  15.    Usage: index input_pathname output_pathname wordlist_pathname
  16. */
  17.  
  18. #include <stdio.h>
  19.  
  20. #define version "1.00"    /* Current version */
  21. #define w_length 30       /* Maximum # of chars in a word
  22.                             Be sure to change fscanf statement in the 
  23.                            function get_list to match this number */
  24.  
  25. #define n_words 250    /* Maximum # of words to index on */
  26. #define max_num 20    /* Maximum # of pages per word */
  27. #define page_width 72   /* Number of columns to use for output */    
  28. #define form_feed 12    /* ASCII form feed character */
  29. #define space 32        /* ASCII space character */
  30.             
  31.  
  32. /* Global Variables */
  33. char words [n_words] [w_length+1];    /* word list */
  34. int w_page [n_words] [max_num];        /* page numbers */
  35. int w_count [n_words];            /* count of page numbers */
  36. int page;                        /* page counter */
  37. int num_words;                          /* actual number of words to index */
  38. char new_word [w_length+1];        /* buffer for 1 word */
  39.  
  40. /* Main Program */
  41. main(argc, argv)
  42. int argc;
  43. char *argv[];
  44. {
  45.         FILE *infile, *outfile, *wordlist, *fopen();
  46.     int a, b, i, n;
  47.  
  48.   /* initialize */
  49.   fprintf(stderr, "INDEX v%s by Greg Haley\n\n", version);
  50.  
  51.   for (a=0; a <= n_words; a++) {
  52.     for (b=0; b <= w_length; b++) words [a] [b] = '\0';
  53.   }
  54.  
  55.   for (a=0; a <= n_words; a++) {
  56.     for (b=0; b <= max_num; b++) w_page [a] [b] = 0;
  57.   }
  58.  
  59.   for (a=0; a <= max_num; a++) w_count [a] = 0;
  60.   for (a=0; a <= max_num; a++) new_word [a] = '\0';
  61.  
  62.   /* open files */
  63.   if (argc != 4) { /* wrong number of args; exit */
  64.     fprintf(stderr, "Usage: index input_pathname output_pathname");
  65.     fprintf(stderr, " wordlist_pathname\n\n");
  66.     fprintf(stderr, "Maximum word length is %d characters\n", w_length);
  67.     fprintf(stderr, "Maximum number of words is %d\n", n_words);
  68.     fprintf(stderr, "Maximum number of pages per word is %d\n", max_num);
  69.     exit(1);
  70.   } else if ((infile = fopen(argv[1], "r")) == NULL) {
  71.     fprintf(stderr, "Can't open input file.\n");
  72.     exit(1);
  73.   } else if ((outfile = fopen(argv[2], "w")) == NULL) {
  74.     fprintf(stderr, "Can't open output file.\n");
  75.     exit(1);
  76.   } else if ((wordlist = fopen(argv[3], "r")) == NULL) {
  77.     fprintf(stderr, "Can't open word list file.\n");
  78.     exit(1);
  79.   } else {
  80.  
  81.     /* Echo pathnames */
  82.     fprintf(stderr, "Input pathname is %s\n", argv[1]);
  83.     fprintf(stderr, "Output pathname is %s\n", argv[2]);
  84.     fprintf(stderr, "Word list pathname is %s\n\n", argv[3]);
  85.  
  86.     /* Read word list */
  87.     num_words = get_list(wordlist); /* Side effects changes global variables */
  88.  
  89.     /* convert word list to lower case */
  90.     for (a=1; a <= num_words; a++) 
  91.       for (b=0; words [a] [b] != '\0'; b++) 
  92.         if ((words [a] [b] >= 'A') && (words [a] [b] <= 'Z')) 
  93.           words [a] [b] += 32;
  94.  
  95.     /* sort word list */
  96.     for (a=1; a < num_words; a++) {
  97.       for (b=a+1; b <= num_words; b++) {
  98.         if ((strcmp (words [a], words [b])) > 0) {
  99.           for (n=0; n <= w_length; n++) {  /* Words [0] is temporary var */
  100.             words [0] [n] = words [a] [n];
  101.             words [a] [n] = words [b] [n];
  102.             words [b] [n] = words [0] [n];
  103.           }
  104.         }
  105.       } 
  106.     }
  107.  
  108.     /* index and write file */
  109.     if (num_words == 0) {
  110.       fprintf (stderr, "No words found in word list file.\n");
  111.       exit(1);  /* Exit program if no words found */
  112.     } else {
  113.       fprintf (stderr, "%d words found in word list file.\n", num_words);
  114.       index_it (infile);  /* Side effects change several global variables */
  115.       print_it (outfile); /* Side effects change several global variables */
  116.     }
  117.  
  118.     /* close files */
  119.     fclose(infile);
  120.     fclose(outfile);
  121.     fclose(wordlist);
  122.   }
  123.  
  124.   /* Normal exit */
  125.   exit(0); 
  126. }
  127.  
  128.  
  129. /* Function to get word list. Global variable words is accessed */
  130. /* Returns number of words found */
  131. get_list (fp)
  132. FILE *fp;
  133. {
  134.     int a, c;
  135.  
  136.   a = 1;
  137.   while (((fscanf(fp, "%30s", words[a])) != EOF) && (a < n_words)) a++;
  138.   return (a-1);
  139. }
  140.  
  141.  
  142. /* Search input file and build index arrays. */
  143. /* All global variables accessed */
  144. index_it (fp)
  145. FILE *fp;
  146. {
  147.     int a, no_match;
  148.     char c;
  149.  
  150.   page = 1;
  151.   while ((next_word(fp)) != EOF) {
  152.     a = 0;
  153.     no_match = 0;
  154.     while ((a < num_words) && (no_match == 0)) {
  155.       a++;
  156.       if ((strcmp (new_word, words [a])) == 0) {
  157.         no_match++;
  158.         if (w_page [a] [w_count[a]] != page) {
  159.           if (w_count [a] == max_num) 
  160.           fprintf(stderr, "number of pages exceeded for word %s\n", words [a]);
  161.           else w_count [a]++;
  162.           w_page [a] [w_count[a]] = page;
  163.         } 
  164.       }
  165.     }
  166.   }
  167.  
  168.   return (0);
  169. }
  170.  
  171.  
  172. /* Get next word from input file. */
  173. /* Increment page number if form feed encountered. */
  174. /* Returns the length of the string or EOF if error or EOF encountered. */
  175. /* Global variables new_word and page accessed. */
  176. next_word (fp)
  177. FILE *fp;
  178. {
  179.     int a, c;
  180.  
  181.   /* skip white spaces */
  182.   do {
  183.     c = fgetc(fp);
  184.     if (c == form_feed) page++;  /* increment page number */
  185.   } while ((c <= space) && (c != EOF));
  186.   if (c != EOF) {
  187.     a = 0;
  188.     if ((c >= 'A') && (c <= 'Z')) c += 32;  /* convert to lower case */
  189.     new_word [a] = c;
  190.  
  191.     /* put chars in string until white space encountered */
  192.     do {
  193.       c = fgetc(fp);
  194.       if ((c > space) && (c != EOF)) {
  195.         a++;
  196.         if ((c >= 'A') && (c <= 'Z')) c += 32;  /* convert to lower case */
  197.         new_word [a] = c;
  198.       } else if (c == form_feed) page++;  /* increment page number */
  199.     } while ((c > space) && (c != EOF) && (a < w_length));
  200.     a++;
  201.     new_word [a] = '\0';  /* don't forget trailing null */
  202.   } 
  203.   if (c == EOF) return (EOF); else return (a-1);
  204. }
  205.  
  206.  
  207. /* Print index from arrays */
  208. /* Global variables accessed but NOT altered. */
  209. print_it (fp)
  210. FILE *fp;
  211. {
  212.     int a, b, i, limit;
  213.   a =0;
  214.   while (a < num_words) {
  215.     a++;
  216.     if (w_count [a] > 0) {
  217.       fprintf (fp, "%s ", words [a]);
  218.       limit = page_width - (strlen (words [a])) - (w_count [a] * 4);
  219.       for (i=1; i <= limit; i++) fprintf (fp, "%c", '.');
  220.       for (b=1; b <= w_count [a]; b++) {
  221.         fprintf (fp, "%3d", w_page [a] [b]);
  222.         if (b < w_count [a]) fprintf (fp, ",");
  223.       } 
  224.       fprintf (fp, "\n");
  225.     }
  226.   }
  227.  
  228.   return (0);
  229. }
  230.